/* UVa- 10189
 * Author- Ajeet Singh
 * http://uva.onlinejudge.org
 * id- ajeet.singh82
 * Contact- ajeet.singh82@gmail.com
 *
 * 
 * Algorithm: 
 *
 *
 * Build:
 * ...
 */
#include <iostream>
#include <iterator> 
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <locale>
using namespace std;
typedef pair<unsigned short, unsigned short> point;
unsigned short M = 0, N = 0;
vector<string> dc;

inline unsigned short  Max(unsigned short x, unsigned short y) {
	return x ^ ((x ^ y) & -(x < y));
}

inline unsigned short Min(unsigned short x, unsigned short y) {
	return y ^ ((x ^ y) & -(x < y));
}


inline char test(const point& T) {
	return dc[T.second][T.first];
}

void process_I(unsigned short M, unsigned short N){
	dc.clear();
	string pixelrow(M, 'O');
	while(N-- > 0) dc.push_back(pixelrow);
}

void apply_color(const point& A, const point& B, char C) {
	dc[A.second][A.first] = C;
	if(A == B)
		return;
	if(A.first < B.first)
		apply_color(point(A.first+1, A.second), B, C);
	if(A.second < B.second)
		apply_color(point(A.first, A.second+1), B, C);
}

void fill_color(const point& A, char P, char C) {
	if(A.first >= M || A.second >= N)
		return;
	if(test(A) != P)
		return;
	dc[A.second][A.first] = C;
	fill_color(point(A.first+1, A.second), P, C);
	fill_color(point(A.first, A.second+1), P, C);
	fill_color(point(A.first-1, A.second), P, C);
	fill_color(point(A.first, A.second-1), P, C);
}

bool process_cmd(const string& cmdline) {
	bool result = false;
	stringstream tokens(stringstream::in | stringstream::out);
	tokens << cmdline;
 	char cmd;
	tokens >> cmd;
	switch(cmd) {
		case 'I':
			tokens >> M >> N;
			if(M > 0 && M <= 250 && N> 0 && N <= 250)
				process_I(M, N);
			else 
				M=N=0;
			break;
		case 'C':
			if(M > 0 && M <= 250 && N> 0 && N <= 250)
				process_I(M, N);
			break;
		case 'L':
			{
				unsigned short X, Y;
				char C;
				tokens >> X >> Y >> C;
				X--; Y--;toupper(C);
				if(X >= 0 && X < M && Y >= 0 && Y < N) {
					point A(X, Y);
					apply_color(A, A, C);
				}
			}
			break;
		case 'V':
			{
				unsigned short X, Y1, Y2;
				char C;
				tokens >> X >> Y1 >> Y2 >> C;
				X--; Y1--; Y2--;toupper(C);
				if(X >= 0 && X < M && Y1 >= 0 && Y1 < N && Y2 >=0 && Y2 < N) {
					point A(X, min(Y1, Y2)), B(X, max(Y1, Y2));
					apply_color(A, B, C);
				}
			}
			break;
		case 'H':
			{
				unsigned short X1, X2, Y;
				char C;
				tokens >> X1 >> X2 >> Y >> C;
				X1--; X2--; Y--;toupper(C);
				if(X1 >= 0 && X1 < M && X2 >= 0 && X2 < M && Y >=0 && Y < N) {
					point A(min(X1, X2), Y), B(max(X1, X2), Y);
					apply_color(A, B, C);
				}
			}
			break;
		case 'K':
			{
				unsigned short X1, X2, Y1, Y2;
				char C;
				tokens >> X1 >> X2 >> Y1 >> Y2 >> C;
				X1--; X2--; Y1--; Y2--;toupper(C);
				if(X1 >= 0 && X1 < M && X2 >= 0 && X2 < M && Y1 >=0 && Y1 < N && Y2 >=0 && Y2 < N) {
					if(X1 < X2 && Y1 < Y2) {
						point A(X1, Y1), B(X2, Y2);
						apply_color(A, B, C);
					}
				}
			}
			break;
		case 'F':
			{
				unsigned short X, Y;
				char C;
				tokens >> X >> Y >> C;
				X--; Y--;toupper(C);
				if(X >= 0 && X < M && Y >=0 && Y < N) {
					point A(X, Y);
					if(test(A) != C)
						fill_color(A, test(A), C);
				}
			}
			break;
		case 'S':
			{	
				string filename;
				tokens >> filename;
				cout << filename << endl;
				copy(dc.begin(), dc.end(), ostream_iterator<string>(cout, "\n"));
			}
			break;
		case 'X':
			result= true;
			break;
		default:
			break;
	}
	return result;
}

/* main
 * */
int main() {
	bool exit = false;
	while ( exit == false ) {
		string cmdline;
		getline(cin, cmdline);
		exit = process_cmd(cmdline);
	}
	return 0;
}
